package com.brogent.android;

import java.sql.Blob;

import android.app.Activity;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Rect;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Contacts;
import android.util.Log;
import android.widget.ImageView;

public class MainActivity extends Activity {
    private final String TAG = this.getClass().getSimpleName();
    private byte[] blob;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Cursor c = managedQuery(Contacts.Photos.CONTENT_URI, new String[] {Contacts.Photos.DATA}, Contacts.ContactMethods.PERSON_ID +"=\'"+ "1" +"\'", null, null); 
        startManagingCursor(c);
        for(int i=0; i<c.getColumnCount(); i++) {
            Log.e(TAG, c.getColumnName(i));
        }

        if (c.moveToFirst()) {
            for(int i=0; i<c.getCount(); i++) {
                try{
                    //ctsJO.put(Constants.CONTACTS_PHOTO, c.getBlob(c.getColumnIndex(Contacts.Photos.DATA)));
                    Log.i(TAG, "  " + c.getBlob(c.getColumnIndex(Contacts.Photos.DATA)));
                    blob = c.getBlob(c.getColumnIndex(Contacts.Photos.DATA));
                } catch(Exception e) {
                    e.printStackTrace();
                } finally {
                    //c.close();
                }
            }
        }

        Bitmap bm;
        //bm = BitmapFactory.decodeByteArray(blob, 0, blob.length);
        bm = BitmapFactory.decodeResource(this.getResources(), R.drawable.background);
        ImageView iv = (ImageView) findViewById(R.id.image);
        iv.setImageBitmap(bm);
        iv.setBackgroundColor(Color.WHITE);
        iv.setPadding(2, 2, 2, 2);

        c.close();

        ImageView iv2 = (ImageView) findViewById(R.id.image2);
        Bitmap bm2;
        //Bitmap.createScaledBitmap(bm, 48, 48, true);
        //iv2.setImageBitmap(Bitmap.createScaledBitmap(bm, 48, 48, true));
        iv2.setImageBitmap(resizeBitmap(bm, 60, 60));
        //iv2.setImageBitmap(bm2);


    }

//    /** resize the bitmap */
//    public Bitmap resizeBitmap(Bitmap bm, int height, int width) {
//        int orgHeight = bm.getHeight();
//        int orgWidth = bm.getWidth();
//
//        Log.i(TAG, "orgHeight = " + orgHeight + ", orgWidth = " + orgWidth);
//
//        float scaleHeight = ((float) height)/orgHeight;
//        float scaleWidth = ((float) width)/orgWidth;
// 
//        // create a matrix for the manipulation
//        Matrix matrix = new Matrix();
//        // resize the bitmap
//        //matrix.postScale(scaleWidth, scaleHeight);
//        matrix.setScale(scaleWidth, scaleHeight);
//
//        // recreate the new Bitmap
//        Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, true);
//        Log.i(TAG, "resHeight = " + resizedBitmap.getHeight() + ", resWidth = " + resizedBitmap.getWidth());
//
//        return resizedBitmap;
//    }

    /** clip the photo, make it a rectangle */
    public Bitmap resizeBitmap(Bitmap bitmap, int targetHeight, int targetWidth) {
        Bitmap srcBitmap;
        Bitmap dstBitmap;
        int left, top, right, down;

        srcBitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);

        int orgHeight = srcBitmap.getHeight();
        int orgWidth = srcBitmap.getWidth();
        int edge = orgHeight > orgWidth ? orgWidth : orgHeight;
        
        dstBitmap = Bitmap.createBitmap(edge, edge, Bitmap.Config.ARGB_8888);


        if(orgWidth > orgHeight) {
            left = Math.abs(orgHeight - orgWidth)/2;
            top = 0;
            right = Math.abs(orgHeight + orgWidth)/2;
            down = orgHeight;
        } else {
            left = 0;
            top = Math.abs(orgHeight - orgWidth)/2;
            right = orgWidth;
            down = Math.abs(orgHeight + orgWidth)/2;
        }
        //Log.v(TAG, "width:" + bitmap.getWidth() + ", height:" + bitmap.getHeight());
        Log.i(TAG, "left = " + left);
        Log.i(TAG, "top = " + top);
        Log.i(TAG, "right = " + right);
        Log.i(TAG, "down = " + down);

        Canvas canvas = new Canvas(dstBitmap);

        //canvas.clipRect(new Rect(left, top, right, down));
        //canvas.clipRect(new Rect(10, 0, edge+10, edge));

        canvas.drawBitmap(srcBitmap, new Rect(left, top, right, down), new Rect(0, 0, edge/2, edge/2), null);

        ImageView iv3 = (ImageView) findViewById(R.id.image3);
        iv3.setImageBitmap(Bitmap.createScaledBitmap(dstBitmap, targetWidth, targetHeight, true));
        return dstBitmap;
    }

}